// 引入env中的url
const evn = require('./evn')
const baseUrl = evn.dev.baseUrl
const subDomain = 'xxx';
const app = getApp()

module.exports = {
  
    request: (url, method, data, checkLogin ) => {
      if (checkLogin) {
        if(wx.getStorageSync('userInfo')==null || wx.getStorageSync('userInfo')==''){
          wx.reLaunch({
            url: '/pages/index/login',
          })
        }
      }
        let _url = `${ baseUrl}/${url}`;
        return new Promise((resolve, reject) => {
            const requestTask = wx.request({
                url: _url,
                data: data,
                method: method,
                success: (res) => {
                    let code = res.data.code;
                    let msg = res.data.msg
                    if (code === 0) {
                      resolve(res.data.data);
                    }
                    else if (code === 201) {
                      wx.reLaunch({
                        url: '/pages/index/login',
                      })
                    } else if (code === 8) {
                      resolve(res.data);
                    } 
                    else {
                      resolve(res.data);
                    }
                },
                fail() {
                    reject('接口有误，请检查')
                }
            });
        });
    },
}
